﻿using SSC.Views;
using SSC.ViewModels;
using SSC.Models; 
using System.Windows;
using System.Windows.Controls;

namespace SSC.Views
{
    /// <summary>
    /// ucUser.xaml 的交互逻辑
    /// </summary>
    public partial class ucUser : UserControl
    {
        public UserViewModel ViewModel { get; set; }

        public ucUser()
        {
            InitializeComponent();
            ViewModel = new UserViewModel(); // 创建 ViewModel 实例
            this.DataContext = ViewModel; // 设置数据上下文为 ViewModel
        }

        // 新增按钮点击事件
        private void AddUserButton_Click(object sender, RoutedEventArgs e)
        {
            // 打开 AddUserWindow
            var addUserWindow = new AddUserWindow();
            if (addUserWindow.ShowDialog() == true)
            {
                // 获取新增的用户信息
                var userName = addUserWindow.UserName;
                var userPassword = addUserWindow.UserPassword;
                var userBackup = addUserWindow.UserBackup;
                var userFace = addUserWindow.UserFace;

                // 将新增用户添加到 Users 列表
                var newUser = new UserModel
                {
                    UserName = userName,
                    UserPassword = userPassword,
                    UserBackup = userBackup,
                    UserFace = userFace
                };
                ViewModel.AddUser(newUser);// 假设 ViewModel 有 Users 集合
            }
        }

        private void DelUserButton_Click(object sender, RoutedEventArgs e)
        {
            // 获取选中的用户
            var selectedUser = ViewModel.SelectedUser;

            if (selectedUser == null)
            {
                MessageBox.Show("请先选择要删除的用户！");
                return;
            }

            // 提示确认删除
            var result = MessageBox.Show($"确认删除用户：{selectedUser.UserName}？", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            if (result != MessageBoxResult.Yes)
                return;

            try
            {
                // 调用 ViewModel 中的删除方法
                ViewModel.DeleteUser(selectedUser);
                MessageBox.Show("删除成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"删除失败：{ex.Message}");
            }
        }
    }
}
